$(function() {
	var $blogName = $("#addBlogFrm").find("input[name='blogName']");
	$blogName.on("keydown", function(e){
		var code = e.which;
		if (code == 27) {
			$blogName.val('');
		}
	});
});
var MarkdownEditorBlogEditPage = {
	'saveBlog':function() {
		if ($("#addBlogFrm").formValidate()) {
			var htmlContent = $("#addBlogFrm").find("textarea[name='mdContent']").val();
			if (StringUtil.isEmpty(htmlContent)) {
				MsgBox.info('内容不能为空！');
				return ;
			}
			addBlogAjax.send();
		}
	},
	'openSwitchBlogTypeDialog':function() {
		MsgBox.open("switchBlogType", "更改博客类型", ctx + "/datum/blog/switchBlogType/mdeditor/index", "600px");
	},
	'openBindBookDialog':function(){
		var blogId = $("#blogId").val();
		MsgBox.open("switchBlogType", "绑定书籍", ctx + "/datum/blog/switchBlogBook/mdeditor/"+blogId+"/index", "400px");
	},
	'switchBlogType':function(node) {
		var blogTypeId = node.codeInfoValue;
		var blogTypeName = node.codeInfoName;
		$("#blogInfo").find("input[name='blogType.codeInfoValue']").val(blogTypeId);
		$("#blogInfo").find('#blogTypeName').text(blogTypeName);
	},
	// 新增博客成功回调
	'onAddBlogSucc':function(data, status, xhr) {
		$.messager.info(data.message);
		// MsgBox.closeActiveDialog();
		var blogId = $("#addBlogFrm").find("input[name='blogId']").val();
		if (StringUtil.isBlank(blogId)) {
			window.location.href = ctx + "/datum/blog/mdBlogUpdate/index/" + data.info;
		}
	},
	'findBlogByIdSucc':function(data, status, xhr) {
		var mdContent = data.info.mdContent;
        delete data.info.mdContent;
        $("#addBlogFrm").autofill(data.info, {
            'formObj':true
        });
        setTimeout(function(){
            var decodeMdContent = Util.htmlDecode(mdContent);
            articleEditor.setMarkdown(decodeMdContent);
        },1500);

	},
	// 关闭编辑器
	'closeEditor':function() {
		var mWindow = MsgBox.getCurrentOpenDialogWindow();
		mWindow.BlogPage.refreshBlogPage();
	},
	// 保存失败
	'onAddError':function() {
		var markdown = articleEditor.getMarkdown();
		var $textarea = $("<textarea/>");
		$textarea.css({
			'z-index': '9999',
			'display': 'block',
			'position': 'absolute',
			'width': '100%',
			'bottom': '0',
			'height': '380px',
		});
		$textarea.text(markdown);
		$(top.document.body).append($textarea);
	},
	'openAddDescDialog':function() {
		var remarks = $("textarea[name='remarks']").val();
		
		var codeRemarks = null;
		if (StringUtil.isBlank(remarks)) {
			codeRemarks = '-';
		} else {
			codeRemarks = Util.encode(remarks);
		}
		MsgBox.open("addDesc", "添加概述", ctx + "/datum/blog/desc/edit/" + codeRemarks + "/index", "400px");
	},
	'setRemarks':function(remarks) {
		$("#addBlogFrm").find("textarea[name='remarks']").val(remarks);
	}
};

